--返回两个值之间的随机数
local seed = os.time()
function Random(min, max)
    seed = seed + 1
	math.randomseed(tostring(seed))
	return math.random(min, max)
end

--四舍五入保留n位小数
function DecimalTo(num, n)
	if not num then
        return "nil";
    end
    return string.format("%."..n.."f", num);
end

--向下取保留2位小数，不足补0
function Price(num)
	if not num then
        return "nil";
    end
	local str = string.format("%.3f", num)
	local len = string.len(str)
    return str:sub(1,len-1);
end

--移除已经require的文件，以便可以重新require新版本
function RemoveRequiredByName(preName)
    for key, _ in pairs(package.preload) do
        if string.find(tostring(key), preName) == 1 then
            package.preload[key] = nil
        end
    end
    for key, _ in pairs(package.loaded) do
        if string.find(tostring(key), preName) == 1 then
            package.loaded[key] = nil
        end
    end
end

--支持替换“.”的string.gsub()方法
function GSub(content, pattern, repl)
	if pattern == "." then
		local str = ""
		for i = 1, string.len(content) do
			local char = string.sub(content, i, i)
			if char == "." then
				char = repl
			end
			str = str .. char
		end
		return str
	end
	return string.gsub(content, pattern, repl)
end

--根据日期获取星期(不传参数则返回今天的星期)
function GetWeek(y, m, d)
	y = y or os.date("%Y")
	m = m or os.date("%m")
	d = d or os.date("%d")
    if m == 1 or m == 2 then
        m = m + 12
        y = y - 1
    end
    local m1, _ = math.modf(3 * (m + 1) / 5)
    local m2, _ = math.modf(y / 4)
    local m3, _ = math.modf(y / 100)
    local m4, _ = math.modf(y / 400)
	return ((d + 2 * m + m1 + y + m2 - m3 + m4) % 7) + 1
end

-- 金额国际化：123456789.5 转成 123,456,789.5
function NumberFormat(numTmp)
    local resultNum = numTmp
    if type(resultNum) == "userdata" then
        --C#的long型需要先转成string才能转number
        resultNum = tostring(resultNum)
    end
    if type(resultNum) == "string" then
        resultNum = tonumber(resultNum)
    end
    if resultNum then
        local inter, point = math.modf(resultNum)
        local strNum = tostring(inter)
        local newStr = ""
        local numLen = string.len(strNum)
        local count = 0
        for i = numLen, 1, -1 do
            if count % 3 == 0 and count ~= 0 then
                newStr = string.format("%s,%s", string.sub(strNum, i, i), newStr)
            else
                newStr = string.format("%s%s", string.sub(strNum, i, i), newStr)
            end
            count = count + 1
        end
        if point > 0 then
            local strPoint = string.format("%.2f", point)
            resultNum = string.format("%s%s", newStr, string.sub(strPoint, 2, string.len(strPoint)))
        else
            resultNum = newStr
        end
        return resultNum
    end
    return numTmp
end